package com.ctgu.j13.controller;

import com.ctgu.j13.service.CustomerService;
import com.ctgu.j13.vo.Customer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @RequestMapping("/save")
    @ResponseBody
    public String saveCustomer()
    {
        //正常来说这里是前端传过来的数据，所以这只是为了方便测试
        Customer customer=new Customer();
//        customer.setCustomerId(5);
        customer.setCustomerName("lll");
        customer.setCustomerGender("女");
        return "插入数据(insert data):"+customerService.save(customer);
    }

    @RequestMapping("/toCustomerList")
    public String toCustomerList(HttpServletRequest request)
    {
        List<Customer> customerList=customerService.customerList();
        if(customerList==null)
            System.out.println("错了");
        else
        {
            for ( Customer s : customerList) {
                System.out.println(s.getCustomerId()+"  "+s.getCustomerName()+"  "+s.getCustomerGender());
            }
        }
        request.setAttribute("customerList",customerList);
        return "customer_list";
    }

    @ResponseBody
    @PostMapping("/delete")
    public String delete(@RequestParam("id") String  id)
    {
        System.out.println(id);
        Integer id1=Integer.valueOf(id);
        customerService.delete(id1);
        System.out.println(id);
        return null;
    }

    @ResponseBody
    @RequestMapping("/batchDelete")
    public String batchDelete(String userList){
        String[] strs = userList.split(",");
        List<Integer> ids = new ArrayList<>();
        for(int i = 0;i<strs.length; i++){
            customerService.delete(Integer.parseInt(strs[i]));
        }
//        JSONObject result=new
//        result.put("success",true);
        return null;
    }

//    @RequestMapping(value = "/toCustomerList" , method = RequestMethod.GET)
//    public String toRegister(){
//        return  "index";
//    }


    @RequestMapping("getAll")
    public String getCustomers(){

        return "";
    }
}
